﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WpfAsteriskCTI
{
    class myLogger
    {
        public static void LogInformation(string slogMessage, bool bactive)
        {
            if (bactive == true)
            {
                try
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory;

                    path = path + "LogFiles\\astLog_" + DateTime.Now.ToLongDateString() + ".txt";
                    //string path = @HostingEnvironment.MapPath("~/LogFiles/ICWebLogFile_" + DateTime.Now.ToLongDateString() + ".txt");

                    // This text is added only once to the file. 
                    if (!File.Exists(path))
                    {
                        // Create a file to write to. 
                        string createText = "LogFile created" + Environment.NewLine;
                        File.WriteAllText(path, createText);
                    }

                    //string appendText = "Information Log Entry : " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + Environment.NewLine;
                    string appendText = "Information Log Entry : " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + " => ";
                    File.AppendAllText(path, appendText);
                    appendText = "  :" + slogMessage + Environment.NewLine;
                    File.AppendAllText(path, appendText);
                    appendText = "-----------------------------------------------------------------" + Environment.NewLine;
                    File.AppendAllText(path, appendText);

                    // Open the file to read from. 
                    //string readText = File.ReadAllText(path);
                    //Console.WriteLine(readText);
                }
                catch (Exception)
                {

                }
            }
        }

        public static void LogError(string slogMessage, bool bactive)
        {
            if (bactive == true)
            {
                try
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory;

                    path = path + "LogFiles\\astLog_" + DateTime.Now.ToLongDateString() + ".txt";
                    //string path = @HostingEnvironment.MapPath("~/LogFiles/ICWebLogFile_" + DateTime.Now.ToLongDateString() + ".txt");

                    // This text is added only once to the file. 
                    if (!File.Exists(path))
                    {
                        // Create a file to write to. 
                        string createText = "LogFile created" + Environment.NewLine;
                        File.WriteAllText(path, createText);
                    }

                    //string appendText = "Information Log Entry : " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + Environment.NewLine;
                    string appendText = "Exception Log Entry : " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString() + " => ";
                    File.AppendAllText(path, appendText);
                    appendText = "  :" + slogMessage + Environment.NewLine;
                    File.AppendAllText(path, appendText);
                    appendText = "*****************************************************************" + Environment.NewLine;
                    File.AppendAllText(path, appendText);

                    // Open the file to read from. 
                    //string readText = File.ReadAllText(path);
                    //Console.WriteLine(readText);
                }
                catch (Exception)
                {

                }
            }
        }
    }

    static class Helpers
    {
        private const int RetriesOnError = 3;
        private const int DelayOnRetry = 1000;

        public static void DeleteOldFiles(uint maximumAgeInDays)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            path = path + "LogFiles\\astLog_" + DateTime.Now.ToLongDateString() + ".txt";
            DateTime minimumDate = DateTime.Now.AddDays(-maximumAgeInDays);
            foreach (var path1 in Directory.EnumerateFiles(path))
            {
                DeleteFileIfOlderThan(path1, minimumDate);
            }
        }

        private static bool IsExcluded(string item, string[] exclusions)
        {
            foreach (string exclusion in exclusions)
            {
                if (item.Equals(exclusion, StringComparison.CurrentCultureIgnoreCase))
                    return true;
            }

            return false;
        }

        private static bool DeleteFileIfOlderThan(string path, DateTime date)
        {
            for (int i = 0; i < RetriesOnError; ++i)
            {
                try
                {
                    FileInfo file = new FileInfo(path);
                    if (file.CreationTime < date)
                        file.Delete();

                    return true;
                }
                catch (IOException)
                {
                    System.Threading.Thread.Sleep(DelayOnRetry);
                }
                catch (UnauthorizedAccessException)
                {
                    System.Threading.Thread.Sleep(DelayOnRetry);
                }
            }

            return false;
        }
    }
}
